﻿using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace ConsolidationTool.WebParts.SubItemEventReceiver
{
    /// <summary>
    /// 列表项事件
    /// </summary>
    public class SubItemEventReceiver : SPItemEventReceiver
    {
       /// <summary>
       /// 正在删除项.
       /// </summary>
       public override void ItemDeleting(SPItemEventProperties properties)
       {
           base.ItemDeleting(properties);
           if (properties.ListTitle.ToUpper().StartsWith("SUB ")) return;
           string parentItemTitle= properties.ListItem.Title;
           string SubList = "Sub "+ properties.ListTitle;

           var subList = properties.Web.Lists.TryGetList(SubList);
           if (subList != null)
           {
               var subItems = subList.GetItems(new SPQuery()
                 {
                     Query = string.Format(@"<Where>
                          <Eq>
                             <FieldRef Name='Title' />
                             <Value Type='Text'>{0}</Value>
                          </Eq>
                       </Where>", parentItemTitle)
                 }); 
               while (subItems.Count > 0)
                   subItems.Delete(0);
               
           }
       }


    }
}
